package com.augurit.smartform.database;


import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import com.augurit.common.util.lang.StringUtils;
import com.augurit.smartform.database.change.ColumnChangeInfo;
import com.augurit.smartform.database.change.ColumnLengthChange;
import com.augurit.smartform.database.change.ColumnModifyInfo;
import com.augurit.smartform.database.change.ColumnNameChange;
import com.augurit.smartform.database.change.ColumnPrecisionChange;
import com.augurit.smartform.database.change.ColumnScaleChange;
import com.augurit.smartform.database.change.CopyDataInfo;
import com.augurit.smartform.database.change.DefaultValueChange;
import com.augurit.smartform.database.change.IsIndexChange;
import com.augurit.smartform.database.change.IsUniqueChange;
import com.augurit.smartform.database.change.NotNullChange;

/**
 * 根据TABLE对象产生SQL语句工具类
 * @author cuigeqiang
 *
 */
public class SQLGenerator {
	private SQLGenerator(){}
	/**
	 * 获得创建表的SQL语句
	 * @param table
	 * @return
	 * cuigeqiang
	 */
	public static String getCreateTable(Table table){
		StringBuffer createTable=new StringBuffer();
		createTable.append("CREATE TABLE ");
		createTable.append(table.getFullName()+" ");
		createTable.append("(");
		List<Column> columns=table.getColumn();
		for(int i=0;i<columns.size();i++){
			Column column=columns.get(i);
			createTable.append(column.getName()+" ");
			createTable.append(column.getDbType()+" ");
			if(StringUtils.isNotBlank(column.getSqlDefaultValue())){
				createTable.append("DEFAULT "+column.getSqlDefaultValue()+" ");
			}
			if(column.isNotNull()){
				createTable.append("NOT NULL ");
			}
			if(i!=columns.size()-1){
				createTable.append(",");
			}
		}
		createTable.append(")");
		return createTable.toString();
	}
	
	/**
	 * 拷贝表数据的SQL语句
	 * @param copyDataInfo
	 * @return
	 * cuigeqiang
	 */
	public static String getCopyTableData(CopyDataInfo copyDataInfo){
		StringBuffer copyTable=new StringBuffer();
		Table newTable=copyDataInfo.getNewTable();
		Table oldTable=copyDataInfo.getOldTable();
		List<Column> columns=copyDataInfo.getColumns();
		StringBuffer colBuff=new StringBuffer();
		for(int i=0;i<columns.size();i++){
			Column column=columns.get(i);
			if(i!=0){
				colBuff.append(",");
			}
			colBuff.append(column.getName());
		}
		copyTable.append("INSERT INTO ");
		copyTable.append(newTable.getFullName());
		copyTable.append("(");
		copyTable.append(colBuff.toString());
		copyTable.append(") ");
		copyTable.append("SELECT ");
		copyTable.append(colBuff.toString()+" ");
		copyTable.append("FROM ");
		copyTable.append(oldTable.getFullName());
		return copyTable.toString();
	}
	
	/**
	 * 拷贝序列的SQL语句
	 * @param copyDataInfo
	 * @return
	 * cuigeqiang
	 */
	public static List<String> getCopySequence(CopyDataInfo copyDataInfo){
		Table newTable=copyDataInfo.getNewTable();
		Table oldTable=copyDataInfo.getOldTable();
		List<String> copySequence=new ArrayList<String>();
		
		copySequence.add(getDropSequence(newTable));
		
		StringBuffer selectOldSequence=new StringBuffer();
		selectOldSequence.append("SELECT ");
		selectOldSequence.append(oldTable.getSequenceName());
		selectOldSequence.append(".NEXTVAL FROM DUAL");
		copySequence.add(selectOldSequence.toString());
		
		StringBuffer newSequece=new StringBuffer();
		newSequece.append("CREATE SEQUENCE ");
		newSequece.append(newTable.getSequenceName()+" ");
		newSequece.append("INCREMENT BY 1 START WITH ? MINVALUE 1 NOMAXVALUE NOCYCLE NOCACHE");
		copySequence.add(newSequece.toString());
		
		return copySequence;
	}
	
	/**
	 * 获得插入值的SQL语句
	 * @param table
	 * @return
	 */
//	public static String getInsertTable(Table table){
//		StringBuffer insertTable=new StringBuffer();
//		if(table.isInserting()){
//			String insertColumn="";
//			String insertValue="";
//			insertColumn+=table.getPkColumn()+",";
//			insertValue+=table.getSequenceName()+".NEXTVAL"+",";
//			List<Column> columns=table.getColumn();
//			for(Column column : columns){
//				if(StringUtils.isNotBlank(column.getValue())){
//					insertColumn+=column.getName()+",";
//					insertValue+=column.getSqlValue()+",";
//				}
//			}
//			insertColumn=insertColumn.substring(0, insertColumn.length()-1);
//			insertValue=insertValue.substring(0, insertValue.length()-1);
//			insertTable.append("INSERT INTO ");
//			insertTable.append(table.getFullName());
//			insertTable.append("(");
//			insertTable.append(insertColumn);
//			insertTable.append(") ");
//			insertTable.append("values");
//			insertTable.append("(");
//			insertTable.append(insertValue);
//			insertTable.append(")");
//		}
//		return insertTable.toString();
//	}
	
	/**
	 * 获得插入值的SQL语句
	 * @param table
	 * @return
	 */
	public static List<String> getInsertTable(Table table){
		List<String> insertList=new ArrayList<String>();
		StringBuffer selectNextSequence=new StringBuffer();
		StringBuffer insertTable=new StringBuffer();
		if(table.isInserting()){
			selectNextSequence.append("SELECT ");
			selectNextSequence.append(table.getSequenceName());
			selectNextSequence.append(".NEXTVAL FROM DUAL");
			
			String insertColumn="";
			String insertValue="";
			insertColumn+=table.getPkColumn()+",";
			insertValue+="?"+",";
			List<Column> columns=table.getColumn();
			for(Column column : columns){
				if(StringUtils.isNotBlank(column.getValue())){
					insertColumn+=column.getName()+",";
					insertValue+=column.getSqlValue()+",";
				}
			}
			insertColumn=insertColumn.substring(0, insertColumn.length()-1);
			insertValue=insertValue.substring(0, insertValue.length()-1);
			insertTable.append("INSERT INTO ");
			insertTable.append(table.getFullName());
			insertTable.append("(");
			insertTable.append(insertColumn);
			insertTable.append(") ");
			insertTable.append("values");
			insertTable.append("(");
			insertTable.append(insertValue);
			insertTable.append(")");
		}
		insertList.add(selectNextSequence.toString());
		insertList.add(insertTable.toString());
		return insertList;
	}
	
	/**
	 * 获得插入值的SQL语句
	 * @param table
	 * @return
	 */
	public static String getSelectTable(Table table){
		StringBuffer selectTable=new StringBuffer();
		if(table.isSelecting()){
			String whereColumn="";
			List<Column> columns=table.getColumn();
			for(Column column : columns){
				if(StringUtils.isNotBlank(column.getValue())){
					String selectColumnName=column.getName();
					if("clob".equalsIgnoreCase(column.getSqlType())){
						selectColumnName="TO_CHAR("+column.getName()+")";
					}
					
					whereColumn+=selectColumnName+" "+column.getSqlQueryValue()+" and ";
				}
			}
			selectTable.append("SELECT * FROM ");
			selectTable.append(table.getFullName()+" ");
			if(StringUtils.isNotBlank(whereColumn)){
				selectTable.append("WHERE ");
				whereColumn=whereColumn.substring(0, whereColumn.length()-4);
				selectTable.append(whereColumn);
			}
		}
		return selectTable.toString();
	}
	
	/**
	 * 通过外键查询到主键ID
	 * @param table
	 * @return
	 * cuigeqiang
	 */
	public static String getSelectTableId(Table table){
		StringBuffer selectTableId=new StringBuffer();
		if(table.isSelecting()){
			String whereColumn="";
			List<Column> columns=table.getColumn();
			for(Column column : columns){
				if(StringUtils.isNotBlank(column.getValue())||column.isForeignKey()){
					String selectColumnName=column.getName();
					whereColumn+=selectColumnName+" "+column.getSqlQueryValue();
				}
			}
			selectTableId.append("SELECT ID FROM ");
			selectTableId.append(table.getFullName()+" ");
			if(StringUtils.isNotBlank(whereColumn)){
				selectTableId.append("WHERE ");
				selectTableId.append(whereColumn);
			}
		}
		return selectTableId.toString();
	}
	
	/**
	 * 获得更新数据的SQL语句
	 * @param table
	 * @return
	 */
	public static String getUpdateTable(Table table){
		StringBuffer updateTable=new StringBuffer();
		if(table.isUpdating()){
			String setPart="";
			String idValue="";
			List<Column> columns=table.getColumn();
			for(Column column : columns){
				if(table.getPkColumn().equalsIgnoreCase(column.getName())){
					idValue=column.getValue();
					continue;
				}
//				if(StringUtils.isNotBlank(column.getValue())){
//					setPart+=column.getName()+"="+column.getSqlValue()+",";
//				}
				setPart+=column.getName()+"="+column.getSqlValue()+",";
			}
			if(StringUtils.isNotBlank(setPart)){
				setPart=setPart.substring(0, setPart.length()-1);
				updateTable.append("UPDATE ");
				updateTable.append(table.getFullName()+" ");
				updateTable.append("SET ");
				updateTable.append(setPart+" ");
				updateTable.append("WHERE ");
				updateTable.append(table.getPkColumn()+"="+idValue);
			}
		}
		return updateTable.toString();
	}
	
	/**
	 * 获得删除数据的SQL语句
	 * @param table
	 * @return
	 */
	public static String getDeleteTable(Table table){
		StringBuffer deleteTable=new StringBuffer();
		if(table.isDeleting()){
			String idValue="";
			List<Column> columns=table.getColumn();
			for(Column column : columns){
				if(table.getPkColumn().equalsIgnoreCase(column.getName())){
					idValue=column.getValue();
					break;
				}
			}
			deleteTable.append("DELETE FROM ");
			deleteTable.append(table.getFullName()+" ");
			deleteTable.append("WHERE ");
			deleteTable.append(table.getPkColumn()+"="+idValue);
		}
		return deleteTable.toString();
	}
	
	/**
	 * 获得删除数据的SQL语句
	 * @param table
	 * @return
	 */
	public static String getDeleteTable(Table table,String[] ids){
		StringBuffer deleteTable=new StringBuffer();
		if(ids!=null&&ids.length>0){
			StringBuffer idsStr=new StringBuffer();
			for(int i=0;i<ids.length;i++){
				if(i!=0){
					idsStr.append(",");
				}
				idsStr.append(ids[i]);
			}
			deleteTable.append("DELETE FROM ");
			deleteTable.append(table.getFullName()+" ");
			deleteTable.append("WHERE ");
			deleteTable.append(table.getPkColumn()+" IN ("+idsStr.toString()+")");
		}
		return deleteTable.toString();
	}
	
	/**
	 * 删除数据表
	 * @param table
	 * @return
	 * cuigeqiang
	 */
	public static String getDropTable(Table table){
		StringBuffer dropTable=new StringBuffer();
		dropTable.append("DROP TABLE ");
		dropTable.append(table.getFullName()+" ");
		return dropTable.toString();
	}
	
	/**
	 * 删除数据表和从表的外键
	 * @param table
	 * @return
	 * cuigeqinag
	 */
	public static String getDropTableCascadeConstraint(Table table){
		StringBuffer dropTableCC=new StringBuffer();
		dropTableCC.append("DROP TABLE ");
		dropTableCC.append(table.getFullName()+" ");
		dropTableCC.append("CASCADE CONSTRAINTS");
		return dropTableCC.toString();
	}
	
	/**
	 * 创建序列的SQL语句
	 * @param table
	 * @return
	 * cuigeqiang
	 */
	public static String getCreateSequence(Table table){
		StringBuffer createSequence=new StringBuffer();
		createSequence.append("CREATE SEQUENCE ");
		createSequence.append(table.getSequenceName()+" ");
		createSequence.append("INCREMENT BY 1 START WITH 1 MINVALUE 1 NOMAXVALUE NOCYCLE NOCACHE");
		return createSequence.toString();
	}
	
	/**
	 * 删除序列的SQL语句
	 * @param table
	 * @return
	 * cuigeqiang
	 */
	public static String getDropSequence(Table table){
		StringBuffer dropSequence=new StringBuffer();
		dropSequence.append("DROP SEQUENCE ");
		dropSequence.append(table.getSequenceName());
		return dropSequence.toString();
	}
	
	/**
	 * 增加数据库列
	 * @param columnChangeInfo
	 * @return
	 * cuigeqiang
	 */
	public static String getAddColumn(ColumnChangeInfo columnChangeInfo){
		StringBuffer addColumn=new StringBuffer();
		addColumn.append("ALTER TABLE ");
		addColumn.append(columnChangeInfo.getNewTable().getFullName()+" ");
		addColumn.append("ADD ");
		addColumn.append("(");
		List<Column> columns=columnChangeInfo.getAddColumns();
		for(int i=0;i<columns.size();i++){
			Column column=columns.get(i);
			addColumn.append(column.getName()+" ");
			addColumn.append(column.getDbType()+" ");
			if(StringUtils.isNotBlank(column.getSqlDefaultValue())){
				addColumn.append("DEFAULT "+column.getSqlDefaultValue()+" ");
			}
			if(column.isNotNull()){
				addColumn.append("NOT NULL ");
			}
			if(i!=columns.size()-1){
				addColumn.append(",");
			}
		}
		addColumn.append(")");
		return addColumn.toString();
	}
	
	/**
	 * 删除数据库列
	 * @param columnChangeInfo
	 * @return
	 * cuigeqiang
	 */
	public static String getDelColumn(ColumnChangeInfo columnChangeInfo){
		StringBuffer delColumn=new StringBuffer();
		delColumn.append("ALTER TABLE ");
		delColumn.append(columnChangeInfo.getNewTable().getFullName()+" ");
		delColumn.append("DROP ");
		delColumn.append("(");
		List<Column> columns=columnChangeInfo.getDelColumns();
		for(int i=0;i<columns.size();i++){
			Column column=columns.get(i);
			delColumn.append(column.getName()+" ");
			if(i!=columns.size()-1){
				delColumn.append(",");
			}
		}
		delColumn.append(")");
		return delColumn.toString();
	}
	
	/**
	 * 重命名数据库列
	 * @param columnChangeInfo
	 * @return
	 * cuigeqiang
	 */
	public static List<String> getRenameColumn(ColumnChangeInfo columnChangeInfo){
		List<String> renameColumns=new ArrayList<String>();
		Map<String,ColumnModifyInfo> modifyColumns=columnChangeInfo.getModifyColumns();
		Iterator<ColumnModifyInfo> it=modifyColumns.values().iterator();
		while(it.hasNext()){
			ColumnModifyInfo columnModifyInfo=it.next();
			ColumnNameChange columnNameChange=columnModifyInfo.getColumnNameChange();
			if(columnNameChange!=null){
				StringBuffer renameColumn=new StringBuffer();
				renameColumn.append("ALTER TABLE ");
				renameColumn.append(columnChangeInfo.getNewTable().getFullName()+" ");
				renameColumn.append("RENAME COLUMN ");
				renameColumn.append(columnNameChange.getOldName()+" ");
				renameColumn.append("TO ");
				renameColumn.append(columnNameChange.getNewName());
				renameColumns.add(renameColumn.toString());
			}
		}
		return renameColumns;
	}
	
	/**
	 * 修改数据库列
	 * @param columnChangeInfo
	 * @return
	 * cuigeqiang
	 */
	public static List<String> getModifyColumn(ColumnChangeInfo columnChangeInfo){
		List<String> modifyColumnList=new ArrayList<String>();
		StringBuffer modColumn=new StringBuffer();
		StringBuffer modColumnContent=new StringBuffer();		
		Map<String,ColumnModifyInfo> modifyColumns=columnChangeInfo.getModifyColumns();
		Iterator<ColumnModifyInfo> it=modifyColumns.values().iterator();
		while(it.hasNext()){
			ColumnModifyInfo columnModifyInfo=it.next();
			Column newColumn=columnModifyInfo.getNewColumn();
			ColumnLengthChange columnLengthChange=columnModifyInfo.getColumnLengthChange();
			ColumnPrecisionChange columnPrecisionChange=columnModifyInfo.getColumnPrecisionChange();
			ColumnScaleChange columnScaleChange=columnModifyInfo.getColumnScaleChange();
			NotNullChange notNullChange=columnModifyInfo.getNotNullChange();
			DefaultValueChange defaultValueChange=columnModifyInfo.getDefaultValueChange();
			IsUniqueChange isUniqueChange=columnModifyInfo.getIsUniqueChange();
			IsIndexChange isIndexChange=columnModifyInfo.getIsIndexChange();
			if(columnLengthChange!=null||
				columnPrecisionChange!=null||
				columnScaleChange!=null||
				notNullChange!=null||
				defaultValueChange!=null
			){
				modColumnContent.append(newColumn.getName()+" ");
				modColumnContent.append(newColumn.getDbType()+" ");
				if(defaultValueChange!=null){
					String newSqlDefaultValue=defaultValueChange.getNewSqlDefaultValue();
					if(newSqlDefaultValue==null){
						modColumnContent.append("DEFAULT NULL ");
					}else{
						modColumnContent.append("DEFAULT "+newColumn.getSqlDefaultValue()+" ");
					}
				}
				if(notNullChange!=null){
					if(newColumn.isNotNull()){
						modColumnContent.append("NOT NULL ");
					}else{
						modColumnContent.append("NULL ");
					}
				}
				modColumnContent.append(",");
			}
			if(isUniqueChange!=null){
				if(newColumn.isUnique()){
					StringBuffer addUnique=new StringBuffer();
					addUnique.append("ALTER TABLE ");
					addUnique.append(columnChangeInfo.getNewTable().getFullName()+" ");
					addUnique.append("ADD CONSTRAINT ");
					addUnique.append("UC_"+columnChangeInfo.getNewTable().getFullName()+"_"+newColumn.getName()+" ");
					addUnique.append("UNIQUE");
					addUnique.append("("+newColumn.getName()+")");
					modifyColumnList.add(addUnique.toString());
				}else{
					StringBuffer dropUnique=new StringBuffer();
					dropUnique.append("ALTER TABLE ");
					dropUnique.append(columnChangeInfo.getNewTable().getFullName()+" ");
					dropUnique.append("DROP CONSTRAINT ");
					dropUnique.append("UC_"+columnChangeInfo.getNewTable().getFullName()+"_"+newColumn.getName());
					modifyColumnList.add(dropUnique.toString());
				}
			}
			if(isIndexChange!=null){
				if(newColumn.isIndex()){
					StringBuffer createIndex=new StringBuffer();
					createIndex.append("CREATE INDEX ");
					createIndex.append("INDEX_"+columnChangeInfo.getNewTable().getFullName()+"_"+newColumn.getName()+" ");
					createIndex.append("ON ");
					createIndex.append(columnChangeInfo.getNewTable().getFullName()+" ");
					createIndex.append("("+newColumn.getName()+")");
					modifyColumnList.add(createIndex.toString());
				}else{
					StringBuffer dropIndex=new StringBuffer();
					dropIndex.append("DROP INDEX ");
					dropIndex.append("INDEX_"+columnChangeInfo.getNewTable().getFullName()+"_"+newColumn.getName());
					modifyColumnList.add(dropIndex.toString());
				}
			}
		}
		if(StringUtils.isNotBlank(modColumnContent.toString())){
			String modColumnContentStr=modColumnContent.substring(0, modColumnContent.length()-1);
			modColumn.append("ALTER TABLE ");
			modColumn.append(columnChangeInfo.getNewTable().getFullName()+" ");
			modColumn.append("MODIFY ");
			modColumn.append("(");
			modColumn.append(modColumnContentStr);
			modColumn.append(")");
			modifyColumnList.add(modColumn.toString());
		}
		return modifyColumnList;
	}
	
	/**
	 * 增加主键约束
	 * @param table
	 * @return
	 * cuigeqiang
	 */
	public static String getAddPrimaryKey(Table table){
		StringBuffer addPrimaryKey=new StringBuffer();
		addPrimaryKey.append("ALTER TABLE ");
		addPrimaryKey.append(table.getFullName()+" ");
		addPrimaryKey.append("ADD CONSTRAINT ");
		addPrimaryKey.append("PK_"+table.getFullName()+" ");
		addPrimaryKey.append("PRIMARY KEY ");
		addPrimaryKey.append("("+table.getPkColumn()+")");
		return addPrimaryKey.toString();
	}
	
	/**
	 * 删除主键约束
	 * @param table
	 * @return
	 * cuigeqiang
	 */
	public static String getDropPrimaryKey(Table table){
		StringBuffer dropPrimaryKey=new StringBuffer();
		dropPrimaryKey.append("ALTER TABLE ");
		dropPrimaryKey.append(table.getFullName()+" ");
		dropPrimaryKey.append("DROP CONSTRAINT ");
		dropPrimaryKey.append("PK_"+table.getFullName());
		return dropPrimaryKey.toString();
	}
	
	/**
	 * 创建所有索引的SQL语句
	 * @param table
	 * @return
	 * cuigeqiang
	 */
	public static List<String> getCreateAllIndex(Table table){
		List<String> createAllIndex=new ArrayList<String>();
		List<String> indexColumns=table.getIndexColumns();
		for(int i=0;i<indexColumns.size();i++){
			String indexColumn=indexColumns.get(i);
			StringBuffer createIndex=new StringBuffer();
			createIndex.append("CREATE INDEX ");
			createIndex.append("INDEX_"+table.getFullName()+"_"+indexColumn+" ");
			createIndex.append("ON ");
			createIndex.append(table.getFullName()+" ");
			createIndex.append("("+indexColumn+")");
			createAllIndex.add(createIndex.toString());
		}
		return createAllIndex;
	}
	
	/**
	 * 删除所有索引的SQL语句
	 * @param table
	 * @return
	 * cuigeqiang
	 */
	public static List<String> getDropAllIndex(Table table){
		List<String> dropAllIndex=new ArrayList<String>();
		List<String> indexColumns=table.getIndexColumns();
		for(int i=0;i<indexColumns.size();i++){
			String indexColumn=indexColumns.get(i);
			StringBuffer dropIndex=new StringBuffer();
			dropIndex.append("DROP INDEX ");
			dropIndex.append("INDEX_"+table.getFullName()+"_"+indexColumn);
			dropAllIndex.add(dropIndex.toString());
		}
		return dropAllIndex;
	}
	
	/**
	 * 添加所有UNIQUE约束
	 * @param table
	 * @return
	 * cuigeqinag
	 */
	public static List<String> getAddAllUnique(Table table){
		List<String> addAllUnique=new ArrayList<String>();
		List<String> uniqueColumns=table.getUniqueColumns();
		for(int i=0;i<uniqueColumns.size();i++){
			String uniqueColumn=uniqueColumns.get(i);
			StringBuffer addUnique=new StringBuffer();
			addUnique.append("ALTER TABLE ");
			addUnique.append(table.getFullName()+" ");
			addUnique.append("ADD CONSTRAINT ");
			addUnique.append("UC_"+table.getFullName()+"_"+uniqueColumn+" ");
			addUnique.append("UNIQUE");
			addUnique.append("("+uniqueColumn+")");
			addAllUnique.add(addUnique.toString());
		}
		return addAllUnique;
	}
	
	/**
	 * 删除所有UNIQUE约束
	 * @param table
	 * @return
	 * cuigeqiang
	 */
	public static List<String> getDropAllUnique(Table table){
		List<String> dropAllUnique=new ArrayList<String>();
		List<String> uniqueColumns=table.getUniqueColumns();
		for(int i=0;i<uniqueColumns.size();i++){
			String uniqueColumn=uniqueColumns.get(i);
			StringBuffer dropUnique=new StringBuffer();
			dropUnique.append("ALTER TABLE ");
			dropUnique.append(table.getFullName()+" ");
			dropUnique.append("DROP CONSTRAINT ");
			dropUnique.append("UC_"+table.getFullName()+"_"+uniqueColumn);
			dropAllUnique.add(dropUnique.toString());
		}
		return dropAllUnique;
	}
	
	/**
	 * 增加所有外键
	 * @param table
	 * @return
	 * cuigeqiang
	 */
	public static List<String> getAddAllForeignKey(Table table){
		List<String> addAllForeignKey=new ArrayList<String>();
		List<FKRelation> allFKRelations=table.getFKRelation();
		for(int i=0;i<allFKRelations.size();i++){
			FKRelation fKRelation=allFKRelations.get(i);
			StringBuffer addForeignKey=new StringBuffer();
			addForeignKey.append("ALTER TABLE ");
			addForeignKey.append(table.getFullName()+" ");
			addForeignKey.append("ADD CONSTRAINT ");
			addForeignKey.append("FK_"+table.getFullName()+"_"+fKRelation.getRefTable()+" ");
			addForeignKey.append("FOREIGN KEY ");
			addForeignKey.append("("+fKRelation.getFkColumn()+") ");
			addForeignKey.append("REFERENCES ");
			addForeignKey.append(fKRelation.getRefTable());
			addForeignKey.append("("+fKRelation.getRefColumn()+")");
			addAllForeignKey.add(addForeignKey.toString());
		}
		return addAllForeignKey;
	}
	
	/**
	 * 删除所有外键约束
	 * @param table
	 * @return
	 * cuigeqiang
	 */
	public static List<String> getDropAllForeignKey(Table table){
		List<String> dropAllForeignKey=new ArrayList<String>();
		List<FKRelation> allFKRelations=table.getFKRelation();
		for(int i=0;i<allFKRelations.size();i++){
			FKRelation fKRelation=allFKRelations.get(i);
			StringBuffer dropForeignKey=new StringBuffer();
			dropForeignKey.append("ALTER TABLE ");
			dropForeignKey.append(table.getFullName()+" ");
			dropForeignKey.append("DROP CONSTRAINT ");
			dropForeignKey.append("FK_"+table.getFullName()+"_"+fKRelation.getRefTable());
			dropAllForeignKey.add(dropForeignKey.toString());
		}
		return dropAllForeignKey;
	}
}
